<template>
  <div>
    <template>
      <el-table :data="model" style="width: 100%">
        <el-table-column prop="_id" label="id" width="250"> </el-table-column>
        <el-table-column prop="url" label="跳转地址" width="180">
        </el-table-column>
        <el-table-column label="广告图片" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.banner" alt="" style="width:10rem">
          </template>
        </el-table-column>
        <el-table-column label="编辑">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="$router.push(`/ad/edit/${scope.row._id}`)">编辑
            </el-button>
            <el-button type="danger" size="small" @click="deleteById(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </div>
</template>

<script>
export default {
  data () {
    return {
      model: []
    }
  },
  created () {
    this.fetch()
  },
  methods: {
    async fetch () {
      const res = await this.$http.get('/common/ad')
      this.model = res.data
    },
    deleteById (row) {
      this.$confirm('此操作将永久删除广告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await this.$http.delete(`/common/ad/${row._id}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetch()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style></style>
